package com.tnovoselec.android.memory.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.tnovoselec.android.memory.Constant;
import com.tnovoselec.android.memory.R;

public class MainFragment extends Fragment implements OnClickListener {

	public static String TAG = MainFragment.class.getSimpleName();

	private Button redButton;
	private Button blueButton;
	private Button greenButton;
	private Button yellowButton;

	//int[] sequence = { 1, 2, 3, 4 };
	List<Integer> sequence = new ArrayList<Integer>();

	private Handler handler = new Handler();
	private boolean isListeningForSequence = false;
	private int counterListener = -1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.main_fragment, container, false);
		redButton = (Button) v.findViewById(R.id.red_button);
		blueButton = (Button) v.findViewById(R.id.blue_button);
		greenButton = (Button) v.findViewById(R.id.green_button);
		yellowButton = (Button) v.findViewById(R.id.yellow_button);
		addClickListeners();
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		handler.postDelayed(restartGame, Constant.PAUSE_DURATION);
	}

	private void playSequence() {
		disableAllButtons();
		for (int i = 0; i < sequence.size(); i++) {
			switch (sequence.get(i)) {
			case Constant.RED:
				handler.postDelayed(turnOnRed, i * Constant.PAUSE_DURATION + i * Constant.PRESSED_DURATION);
				break;
			case Constant.BLUE:
				handler.postDelayed(turnOnBlue, i * Constant.PAUSE_DURATION + i * Constant.PRESSED_DURATION);
				break;
			case Constant.GREEN:
				handler.postDelayed(turnOnGreen, i * Constant.PAUSE_DURATION + i * Constant.PRESSED_DURATION);
				break;
			case Constant.YELLOW:
				handler.postDelayed(turnOnYellow, i * Constant.PAUSE_DURATION + i * Constant.PRESSED_DURATION);
				break;

			default:
				break;
			}
		}
		handler.postDelayed(startListeningForSequence, sequence.size() * Constant.PAUSE_DURATION + sequence.size() * Constant.PRESSED_DURATION);
	}

	private void turnOnBlueButton() {
		if (blueButton != null) {
			blueButton.setPressed(true);
		}
		handler.postDelayed(turnOffAll, Constant.PRESSED_DURATION);
	}

	private void turnOnGreenButton() {
		if (greenButton != null) {
			greenButton.setPressed(true);
		}
		handler.postDelayed(turnOffAll, Constant.PRESSED_DURATION);
	}

	private void turnOnYellowButton() {
		if (yellowButton != null) {
			yellowButton.setPressed(true);
		}
		handler.postDelayed(turnOffAll, Constant.PRESSED_DURATION);
	}

	private void turnOnRedButton() {
		if (redButton != null) {
			redButton.setPressed(true);
		}
		handler.postDelayed(turnOffAll, Constant.PRESSED_DURATION);
	}

	private void turnOffAllButtons() {
		if (blueButton != null) {
			blueButton.setPressed(false);
		}
		if (greenButton != null) {
			greenButton.setPressed(false);
		}
		if (yellowButton != null) {
			yellowButton.setPressed(false);
		}
		if (redButton != null) {
			redButton.setPressed(false);
		}
	}

	Runnable turnOnRed = new Runnable() {

		@Override
		public void run() {
			turnOnRedButton();

		}
	};
	Runnable turnOnBlue = new Runnable() {

		@Override
		public void run() {
			turnOnBlueButton();

		}
	};
	Runnable turnOnGreen = new Runnable() {

		@Override
		public void run() {
			turnOnGreenButton();

		}
	};
	Runnable turnOnYellow = new Runnable() {

		@Override
		public void run() {
			turnOnYellowButton();

		}
	};
	Runnable turnOffAll = new Runnable() {

		@Override
		public void run() {
			turnOffAllButtons();

		}
	};
	Runnable startListeningForSequence = new Runnable() {

		@Override
		public void run() {
			isListeningForSequence = true;
			enableAllButtons();
		}
	};
	Runnable restartGame = new Runnable() {

		@Override
		public void run() {
			isListeningForSequence = false;
			counterListener = -1;
			sequence.clear();
			sequence.add(generateNextInSequence());
			playSequence();
		}
	};

	Runnable proceedGame = new Runnable() {

		@Override
		public void run() {
			isListeningForSequence = false;
			counterListener = -1;
			sequence.add(generateNextInSequence());
			playSequence();
		}
	};

	private void disableAllButtons() {
		if (blueButton != null) {
			blueButton.setEnabled(false);
		}
		if (greenButton != null) {
			greenButton.setEnabled(false);
		}
		if (yellowButton != null) {
			yellowButton.setEnabled(false);
		}
		if (redButton != null) {
			redButton.setEnabled(false);
		}
	}

	private void enableAllButtons() {
		if (blueButton != null) {
			blueButton.setEnabled(true);
		}
		if (greenButton != null) {
			greenButton.setEnabled(true);
		}
		if (yellowButton != null) {
			yellowButton.setEnabled(true);
		}
		if (redButton != null) {
			redButton.setEnabled(true);
		}
	}

	private void addClickListeners() {
		if (blueButton != null) {
			blueButton.setOnClickListener(this);
		}
		if (greenButton != null) {
			greenButton.setOnClickListener(this);
		}
		if (yellowButton != null) {
			yellowButton.setOnClickListener(this);
		}
		if (redButton != null) {
			redButton.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		counterListener++;
		if (!isListeningForSequence) {
			return;
		}
		if (counterListener == sequence.size()-1){
			disableAllButtons();
		}
		boolean isOK = false;
		if (v == redButton) {
			if (sequence.get(counterListener) != Constant.RED) {
				Toast.makeText(getActivity(), "Kita", Toast.LENGTH_LONG).show();
				handler.postDelayed(restartGame, 2000);
			} else {
				isOK = true;
			}
		} else if (v == blueButton) {
			if (sequence.get(counterListener) != Constant.BLUE) {
				Toast.makeText(getActivity(), "Kita", Toast.LENGTH_LONG).show();
				handler.postDelayed(restartGame, 2000);
			} else {
				isOK = true;
			}
		} else if (v == greenButton) {
			if (sequence.get(counterListener) != Constant.GREEN) {
				Toast.makeText(getActivity(), "Kita", Toast.LENGTH_LONG).show();
				handler.postDelayed(restartGame, 2000);
			} else {
				isOK = true;
			}
		} else if (v == yellowButton) {
			if (sequence.get(counterListener) != Constant.YELLOW) {
				Toast.makeText(getActivity(), "Kita", Toast.LENGTH_LONG).show();
				handler.postDelayed(restartGame, 2000);
			} else {
				isOK = true;
			}
		}
		if (isOK && counterListener == sequence.size() - 1) {
			//Toast.makeText(getActivity(), "bravo!", Toast.LENGTH_LONG).show();
			handler.postDelayed(proceedGame, 2000);
		}
	}

	private int generateNextInSequence() {
		Random rand = new Random(System.currentTimeMillis());
		int next = rand.nextInt(4);
		return next;
	}

	private List<Integer> generateTestSequence() {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < 4; i++) {
			list.add( generateNextInSequence());

		}
		//Log.i(TAG, "" + seq[0] + "," + seq[1] + "," + seq[2] + "," + seq[3]);
		return list;
	}
}
